import MallCardBlot from './blot/formats/mall-card'
import VideoCardBlot from './blot/formats/video-card'
import LiveCardBlot from './blot/formats/live-card'
import ArticleCardBlot from './blot/formats/article-card'
import NativeImageBlot, { NativeImageCreateValue } from './blot/formats/native-image'
import CutOffBlot from './blot/formats/cut-off'
import Quill from 'quill'
import { ShallowRef } from 'vue'
import { CardValue } from './blot/card'


const LinkBlot = Quill.import('formats/link')
const Delta = Quill.import('delta')
const lt = '\n'
const cardFormatInsertConfigList = [
    { name: 'insertMallCard', blotName: 'mall-card' }, //会员购
    { name: 'insertVideoCard', blotName: 'video-card' }, //视频
    { name: 'insertLiveCard', blotName: 'live-card' }, //直播
    { name: 'insertArticleCard', blotName: 'article-card' }, //文章
]

/** 初始化quill，注册blot，全局只需要调用一次 */
export const initQuillRegister = () => {
    LinkBlot.className = 'link' //创建链接时会附加link的className
    // 使用Quill.register 是全局注入，如果需要局部注入，请使用 new Parchment.Registry()？ quill1.x 不支持
    Quill.register('formats/native-image', NativeImageBlot)
    Quill.register('formats/cut-off', CutOffBlot)
    Quill.register('formats/mall-card', MallCardBlot)
    Quill.register('formats/video-card', VideoCardBlot)
    Quill.register('formats/live-card', LiveCardBlot)
    Quill.register('formats/article-card', ArticleCardBlot)
}

/** 初始每个插入的blot方法 */
export const initQuillFormat = (QuillEditor: ShallowRef<Quill | undefined>) => {
    Reflect.set(
        QuillEditor.value!,
        'insertImages',
        (imageValues: NativeImageCreateValue[]) => {
            const selection = QuillEditor.value?.getSelection(true)!
            let index = selection.index
            const delta = new Delta()
                .retain(index)
                .delete(selection.length)
                .insert(lt)
            index++
            imageValues.forEach((imageOpt) => {
                delta.insert({ 'native-image': imageOpt }).insert(lt)
                index += 2
            })
            QuillEditor.value?.updateContents(delta)
            QuillEditor.value?.setSelection(index, 0)
        }
    )
    Reflect.set(
        QuillEditor.value!,
        'insertCutOff',
        (cutOffValue: { type: number }) => {
            const selection = QuillEditor.value?.getSelection(true)!
            let index = selection.index
            const delta = new Delta()
                .retain(index)
                .delete(selection.length)
                .insert(lt)
            delta.insert({
                'cut-off': cutOffValue,
            })
            index += 3
            QuillEditor.value?.updateContents(delta)
            QuillEditor.value?.setSelection(index, 0)
        }
    )
    cardFormatInsertConfigList.forEach((item) => {
        Reflect.set(QuillEditor.value!, item.name, (cardValue: CardValue) => {
            const selection = QuillEditor.value?.getSelection(true)!
            let index = selection.index
            const delta = new Delta()
                .retain(index)
                .delete(selection.length)
                .insert(lt)
            index++
            delta.insert({ [item.blotName]: cardValue }).insert(lt)
            index += 2
            QuillEditor.value?.updateContents(delta)
            QuillEditor.value?.setSelection(index, 0)
        })
    })
}